package com.esunny.resourcevisualization.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.esunny.resourcevisualization.domain.entity.CustomizeColumn;
import com.esunny.resourcevisualization.domain.entity.ResourceTypeField;
import com.esunny.resourcevisualization.manage.CustomizeColumnManage;
import com.esunny.resourcevisualization.manage.ResourceTypeFieldManage;
import com.esunny.resourcevisualization.manage.SystemConfigManage;
import com.esunny.resourcevisualization.service.CustomizeColumnService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0.0.0
 * @ClassName CustomizeColumnServiceImpl
 * @Description 自定义表头逻辑处理
 * @Author baochencong
 * @Date 2021/3/22 11:06
 */
@Service
public class CustomizeColumnServiceImpl implements CustomizeColumnService {

    @Resource
    private SystemConfigManage systemConfigManage;

    @Resource
    private ResourceTypeFieldManage resourceTypeFieldManage;

    @Resource
    private CustomizeColumnManage customizeColumnManage;

    @Override
    public boolean addCustomizeColumn(CustomizeColumn customizeColumn) {
        return customizeColumnManage.addCustomizeColumn(customizeColumn);
    }

    @Override
    public boolean deleteCustomizeColumn(String username,String resourceType) {
        return customizeColumnManage.deleteCustomizeColumn(username,resourceType);
    }

    @Override
    public boolean updateCustomizeColumn(CustomizeColumn customizeColumn) {
        if (judgeCustomizeColumnNull(customizeColumn.getUsername(),customizeColumn.getResourceType())){
            return customizeColumnManage.addCustomizeColumn(customizeColumn);
        }
        return customizeColumnManage.updateCustomizeColumn(customizeColumn);
    }

    @Override
    public JSONArray getCustomizeColumn(String username,String resourceType) {
        JSONArray columns = customizeColumnManage.getCustomizeColumn(username,resourceType);
        if (columns == null) {
            columns = customizeColumnManage.getCustomizeColumn("default",resourceType);
        }
        return columns;
    }

    @Override
    public boolean generateDefaultColumn(String resourceType) {
        String prefix = "[{\"label\":\"实体id\",\"prop\":\"resourceTypeEntityId\",\"width\":\"60\",\"key\":\"\",\"sort\":\"true\",\"align\":\"center\",},\n{\"label\":\"实体描述\",\"prop\":\"resourceTypeEntityDescription\",\"width\":\"\",\"key\":\"\",\"sort\":\"false\",\"align\":\"center\",},";
        String suffix = "{\"label\":\"实体状态\",\"prop\":\"status\",\"width\":\"120\",\"key\":\"\",\"sort\":\"true\",\"align\":\"center\"},{\"label\":\"使用人\",\"prop\":\"owner\",\"width\":\"120\",\"key\":\"\",\"sort\":\"true\",\"align\":\"center\"},{\"label\":\"创建人\",\"prop\":\"createdBy\",\"width\":\"120\",\"key\":\"\",\"sort\":\"true\",\"align\":\"center\",},\n{\"label\":\"修改人\",\"prop\":\"lastEditedBy\",\"width\":\"120\",\"key\":\"\",\"sort\":\"true\",\"align\":\"center\",},\n{\"label\":\"是否可申请\",\"prop\":\"mayApply\",\"width\":\"120\",\"key\":\"\",\"sort\":\"true\",\"align\":\"center\",},]";

        List<ResourceTypeField> resourceTypeFieldList = resourceTypeFieldManage.getResourceTypeFieldByName(resourceType);

        String defaultColumnJSON = getDefaultColumnJSON(prefix, suffix, resourceTypeFieldList);

        CustomizeColumn customizeColumn = new CustomizeColumn();
        customizeColumn.setUsername("default");
        customizeColumn.setResourceType(resourceType);
        customizeColumn.setColumns(defaultColumnJSON);

        System.out.println(customizeColumn);
        if (customizeColumnManage.getCustomizeColumn("default",resourceType) == null){
            return customizeColumnManage.addCustomizeColumn(customizeColumn);
        }

        return customizeColumnManage.updateCustomizeColumn(customizeColumn);
    }

    public String getDefaultColumnJSON(String prefix,String suffix,List<ResourceTypeField> resourceTypeFieldList){
        String result = "";

        result += prefix;
        for (ResourceTypeField item:resourceTypeFieldList) {
            String temp = "{\"label\":"+ "\""+item.getResourceTypeFieldTitle()+"\"" + ",";
            temp += "\"prop\":"+ "\""+item.getResourceTypeFieldName()+ "\"" + ",";
            temp += "\"width\":\"\",\"key\":\"\",\"sort\":\"true\",\"align\":\"center\",},";
            result += temp;
        }
        result += suffix;

        return result;
    }

    /**
     * 判断用户是否自定义列信息
     *
     * @param username 用户名
     * @return 是否自定义
     */
    public boolean judgeCustomizeColumnNull(String username,String resourceType) {
        JSONArray columns = customizeColumnManage.getCustomizeColumn(username,resourceType);
        if (columns == null) {
            return true;
        }
        return false;
    }
}
